<?php

/**
 * User: lsx
 * Date: 2017/5/8
 * Time: 14:35
 * Progress:0%
 */
namespace logic\wechat\index;
use library\common\str;

use model\Account;
use model\ShopConfig;
use model\ShopRegUserOrder;

use model\ShopStore;
use model\ShopStoreDelivery;

use think\Config;
use think\Cookie;
use think\Db;

class LAccount
{
    private $shopWatercardOrder;
    public function __construct()
    {

    }

    public function logic_reg_delivery(){
        $a_id= Cookie::get(Config::get('const.cookie_name_wechat'));
        $model2=new ShopStoreDelivery();
        if($model2->where(['account_id'=>$a_id])->find()){
            return true;
        }
        return false;
    }

    public function logic_store_data(){
//        $a_id= Cookie::get(Config::get('const.cookie_name_wechat'));

        $model=new ShopStore();
        $data=$model->field('id,name')->order('id asc')->select();
        return $data;
    }


    public function logic_register_delivery_submit($param){
        $a_id= Cookie::get(Config::get('const.cookie_name_wechat'));
        $insert_data1=array(
            'name'=>$param['name'],
            'mobile'=>$param['mobile'],
            'wechat'=>$param['wechat']
        );
        $model1=new Account();
        Db::startTrans();
        $res1=$model1->validate("Account.edit")->save($insert_data1,['id'=>$a_id]);
        if(!$res1) {
            Db::rollback();
            return array(
                'success'=>false,
                'message'=>'数据操作错误1，请稍后再试',
            );
        };
        $insert_data2=array(
            'account_id'=>$a_id,
            'store_id'=>$param['store_id']
        );
        $model2=new ShopStoreDelivery();
        if($model2->where(['account_id'=>$a_id,'store_id'=>$param['store_id']])->find()){
            Db::rollback();
            return array(
                'success'=>false,
                'message'=>'数据操作错误2，请稍后再试',
            );
        }
        $res2=$model2->validate(true)->save($insert_data2);
        if(!$res2){
            Db::rollback();
            return array(
                'success'=>false,
                'message'=>'数据操作错误3，请稍后再试',
            );
        }
        Db::commit();
        return array(
            'success'=>true,
            'message'=>'注册成功，请耐心等待审核',
        );



    }

    public function logic_register_result(){
        $a_id= Cookie::get(Config::get('const.cookie_name_wechat'));
//        $a_id=23;
        $model=new ShopStoreDelivery();
        $res=$model->alias('s')->join('shop_store t','t.id = s.store_id','left')->field('s.*,t.name as store_name')->where(['account_id'=>$a_id])->find();
        if($res){
            return $res->getData();
        }else{
            return false;
        }
//        var_dump($flag_status);die();
    }

    public function logic_reg_intro(){

    }

    public function logic_reg_user(){
        $model=new ShopConfig();
        $data=$model->find();
        return $data->getData();
    }

    public function logic_reg_vip(){

    }



    //购买vip会员，下单
    public function logic_create_reg_vip_order($param){
        $a_id= Cookie::get(Config::get('const.cookie_name_wechat'));

        //vip卡的id
        $shopVip=new ShopWatercard();
        $info=$shopVip->where(['id'=>$param['watercard_id']])->find();
        if(!$info){
            return array(
                'success'=>false,
                'message'=>'数据出错1，请稍后再试',
            );
        }

        $insert_data=array(
            'card_id'=>$info['id'],
            'card_count'=>$info['count'],
            'type'=>0,
            'account_id'=>$a_id,
            'order_sn'=>str::orderNo(),
            'order_pay'=>$info->pay_money,
            'order_time'=>time(),
            'flag_pay'=>0,
            'remark'=>'购买vip会员'
        );

        return array(
            'success'=>true,
            'message'=>'下单成功',
            'data'=>array('order_id'=>$res)
        );

    }

}